package cn.s1995.sloth.pay.utils;

import cn.hutool.core.map.MapUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;

/**
 * @author sloth
 * @date 2019-05-30
 * <p>
 * 支付渠道名称
 */
@AllArgsConstructor
public enum PayChannelNameEnum {

	/**
	 * Paypal支付
	 */
	PAYPAL_PAY("PAYPAL_PAY", "Paypal支付"),

	/**
	 * Stripe支付
	 */
	STRIPE_PAY("STRIPE_PAY", "Stripe支付"),

	/**
	 * 支付宝wap支付
	 */
	ALIPAY_WAP("ALIPAY_WAP", "支付宝手机支付"),

	/**
	 * 微信H5支付
	 */
	WEIXIN_WAP("WEIXIN_WAP", "微信H5支付"),

	/**
	 * 微信公众号支付
	 */
	WEIXIN_MP("WEIXIN_MP", "微信公众号支付"),

	/**
	 * 聚合支付
	 */
	MERGE_PAY("MERGE_PAY", "yungouos 一码付");

	/**
	 * 名称
	 */
	@Getter
	private String name;

	/**
	 * 描述
	 */
	private String description;

	/**
	 * 通过ua 判断所属渠道
	 * @param ua 浏览器类型
	 * @return
	 */
	public static Enum getChannel(String ua) {
		if (ua.contains(PayConstants.ALIPAY)) {
			return PayChannelNameEnum.ALIPAY_WAP;
		}
		else if (ua.contains(PayConstants.MICRO_MESSENGER)) {
			return PayChannelNameEnum.WEIXIN_MP;
		}
		else if (ua.contains(PayConstants.PAYPAL)) {
			return PayChannelNameEnum.PAYPAL_PAY;
		}
		else if (ua.contains(PayConstants.STRIPE)) {
			return PayChannelNameEnum.STRIPE_PAY;
		}
		else {
			return PayChannelNameEnum.MERGE_PAY;
		}
	}

	/**
	 * 支付方式所属渠道
	 */
	private final static Map<Integer, PayChannelNameEnum> payMethodChannelMap = MapUtil.newHashMap();

	/**
	 * 渠道所属支付方式
	 */
	private final static Map<PayChannelNameEnum, Integer> channelPayMethodMap = MapUtil.newHashMap();

	static {
		payMethodChannelMap.put(PayConstants.PayMethod.wechat, PayChannelNameEnum.WEIXIN_MP);
		payMethodChannelMap.put(PayConstants.PayMethod.alipay, PayChannelNameEnum.ALIPAY_WAP);
		payMethodChannelMap.put(PayConstants.PayMethod.paypal, PayChannelNameEnum.PAYPAL_PAY);
		payMethodChannelMap.put(PayConstants.PayMethod.stripe, PayChannelNameEnum.STRIPE_PAY);

		channelPayMethodMap.put(PayChannelNameEnum.WEIXIN_MP, PayConstants.PayMethod.wechat);
		channelPayMethodMap.put(PayChannelNameEnum.ALIPAY_WAP, PayConstants.PayMethod.alipay);
		channelPayMethodMap.put(PayChannelNameEnum.PAYPAL_PAY, PayConstants.PayMethod.paypal);
		channelPayMethodMap.put(PayChannelNameEnum.STRIPE_PAY, PayConstants.PayMethod.stripe);
	}

	/**
	 * 通过支付方式获取支付渠道
	 * @param payMethod 支付方式
	 * @return
	 */
	public static PayChannelNameEnum getChannel(int payMethod) {
		return payMethodChannelMap.containsKey(payMethod) ? payMethodChannelMap.get(payMethod) : PayChannelNameEnum.PAYPAL_PAY;
	}

	/**
	 * 通过渠道获取支付方式
	 * @param channelNameEnum 渠道
	 * @return
	 */
	public static int getPayMethod(PayChannelNameEnum channelNameEnum) {
		return channelPayMethodMap.containsKey(channelNameEnum) ? channelPayMethodMap.get(channelNameEnum) : PayConstants.PayMethod.paypal;
	}

}
